package com.dianda.generic.service.testService;

import com.alibaba.fastjson.JSONObject;
import com.dianda.generic.commons.componet.BaseDaoComponent;
import com.dianda.generic.commons.componet.RedisComponent;
import com.dianda.generic.commons.utils.BaseChecks;
import com.dianda.generic.commons.utils.CollectionUtils;
import com.dianda.generic.commons.utils.ResponseUtils;
import com.dianda.generic.dao.bean.*;
import com.dianda.generic.dao.pojo.LoginInfo;
import com.dianda.generic.datasource.DataSourceNames;
import com.dianda.generic.datasource.DataSourceTypes;
import com.dianda.generic.datasource.annotation.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Create by The_Answer on 2018/4/4.
 */

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private BaseDaoComponent baseDaoComponent;

    @Autowired
    private RedisComponent redisComponent;

    @Autowired
    private Environment env;

    @Override
    @DataSource(name = DataSourceNames.FIRST) // 指定数据源，不指定默认第一个数据源
    public JSONObject queryTest(Map<String, Object> params) throws Exception {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        QueryParams listUser = QueryParams.createQueryParams("t_user");
        params.put("offset", "0");
        params.put("limit", "3");
        params.put("database",DataSourceTypes.MYSQL); // 指定数据库分页，MYSQL时可不写
        listUser.addQueryParams(Parameter.createParameter("t_address", "3"));
        listUser = BaseChecks.addLikeParams("t_name", "3", listUser);//菜单显示名称 模糊查询
        listUser.addOrderColumns("t_address asc");
        listUser.addQueryParamsByMap(params);
        listUser.printSelf();
        List<Map<String, Object>> userList = baseDaoComponent.selectDataByParams(listUser);
        return ResponseUtils.createSuccessResponseBody("查询成功", userList);
    }


    @Override
    @DataSource(name = DataSourceNames.FIRST) // 指定数据源，不指定默认第一个数据源
    @Transactional(rollbackFor = Exception.class) // 指定事物
    public JSONObject insertTest(Map<String, Object> params) throws Exception {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        InsertParams insertButton = InsertParams.createInsertParams("t_user");
        params.put("t_id", "11");
        params.put("t_name", 11);
        params.put("t_age", 11);
        params.put("t_address", "11");
        insertButton.addParamsForMap(CollectionUtils.removeEmptyKeyByParams(params));
        baseDaoComponent.insertDataByParams(insertButton);

        return ResponseUtils.createSuccessResponseBody("新增成功");
    }

    @Override
    @DataSource(name = DataSourceNames.SECOND) // 指定数据源，不指定默认第一个数据源
    public JSONObject updateTest(Map<String, Object> params) throws Exception {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        UpdateParams updateById = UpdateParams.createUpdateParams("t_user");
        updateById.addParamsForMap(CollectionUtils.removeEmptyKeyByParams(params));
        updateById.addWhereParameter(Parameter.createParameter("t_id", "2"));
        baseDaoComponent.updateDataByParams(updateById);
        return ResponseUtils.createSuccessResponseBody("修改成功");
    }


    @Override
    @DataSource(name = DataSourceNames.THIRD) // 指定数据源，不指定默认第一个数据源
    public JSONObject deleteTest(Map<String, Object> params) throws Exception {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        DeleteParams deleteById = DeleteParams.createDeleteParams("t_user", Parameter.createParameter("t_id", "1"));
        deleteById.printSelf();
        baseDaoComponent.deleteDataByParams(deleteById);

        return ResponseUtils.createSuccessResponseBody("删除成功");
    }

}
